const { Sequelize, DataTypes } = require("sequelize");
const config = {
    host: "localhost", //主机名
    database: "music", //使用的哪个数据库名
    username: "root", //账号
    password: "root", //密码
    port: 3306, //端口号，mysql默认3306
};
const sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    {
        host: config.host,
        dialect: "mysql",
        pool: {
            max: 5, //连接池最大连接数量
            min: 0, //最小连接数量
            idle: 10000, //如果一个线程 10秒内么有被使用过的话，就释放
        },
        //格式化时间
        dialectOptions: {
            dateStrings: true,
            typeCast: true
        },
        logging: true,
        timezone: '+08:00' //改为标准时区
    }
);
const User = require("./tables/user")(sequelize, DataTypes);
const Song = require("./tables/song")(sequelize, DataTypes);
const Singer = require("./tables/singer")(sequelize, DataTypes);


//模型关系
//1.歌手在歌曲表中添加singerId
//歌手在歌曲表中添加singerId 指向歌曲表的singerId，因为singerId为主键
Singer.hasMany(Song, {
    foreignKey: 'singerId',
});
Song.belongsTo(Singer, {
    foreignKey: 'singerId',
    targetKey: 'singerId'
})



module.exports = { sequelize, User, Song, Singer };













